//
// Created by 86152 on 2021/7/30.
//

#ifndef DATASTRUCTURE_STL_TEST_H
#define DATASTRUCTURE_STL_TEST_H

#include <array>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <list>
#include <iterator>
#include <iostream>
namespace stl_test{
    void test_stack(){
        std::stack<int> stack;
        for (int i = 0; i < 10; ++i) {
            stack.push(i);
        }
        for (int j = 0; j < 10; ++j) {
            std::cout << stack.top() << " ";
            stack.pop();
        }
        std::cout << std::endl;
    }

    void test_queue(){
        std::queue<int> queue;
        for (int i = 0; i < 10; ++i) {
            queue.push(i);
        }
        for (int j = 0; j < 10; ++j) {
            std::cout << queue.front() << " ";
            queue.pop();
        }
        std::cout << std::endl;
    }

    void test_priority_queue(){
        std::priority_queue<int> priorityQueue;
        for (int i = 0; i < 10; ++i) {

            priorityQueue.push(rand() % 30);
        }

        for (int j = 0; j < 10; ++j) {
            std::cout << priorityQueue.top() << " ";
            priorityQueue.pop();
        }
        std::cout << std::endl;
    }

    void test_array(){
        std::array<int,10> array = {1,2,3};
        array.fill(10);
        for (int i = 0; i < array.size(); ++i) {
            std::cout << array.at(i) << " ";
        }
        std::cout << std::endl;
    }

    void test_vector(){
        std::vector<char> vector(3,'a');
        vector.push_back('s');
        vector.pop_back();
        vector.assign(20,'x');
        for (const auto &item : vector) {
            std::cout << item;
        }
        std::cout << std::endl;

        vector.clear();
        for (int i = 65; i < 75; ++i) {
            vector.push_back(i);
        }
        vector.erase(vector.begin() + 5,vector.begin() + 7); // 删除 F G
        for (const auto &item : vector) {
            std::cout << item << " ";
        }
    }

    void test_deque(){
        std::deque<char> deque(3,'a');
        deque.push_back('s');
        deque.pop_back();
        deque.assign(20,'x');
        for (const auto &item : deque) {
            std::cout << item;
        }
        std::cout << std::endl;

        deque.clear();
        for (int i = 65; i < 75; ++i) {
            deque.push_back(i);
        }
        deque.erase(deque.begin() + 5,deque.begin() + 7); // 删除 F G
        // 在队头添加FG
        deque.push_front('G');
        deque.push_front('F');
        for (const auto &item : deque) {
            std::cout << item << " ";
        }
    }

    void test_list(){
        std::list<char> list;
        for (int i = 65; i < 75; ++i) {
            list.push_back(i);
        }
        auto end = list.begin();
        auto begin = list.begin();
        for (int j = 0; j < 5; ++j) {
            begin++;
            end++;
        }
        end++;end++;
        list.erase(begin,end); // 删除 F G
        // 在队头添加FG
        list.push_front('G');
        list.push_front('F');
        for (const auto &item : list) {
            std::cout << item << " ";
        }
        std::cout << std::endl;

        list.clear();
        list.reverse();
        for (const auto &item : list) {
            std::cout << item << " ";
        }
        std::cout << std::endl;
    }

    void test_iterator(){
        std::list<int> list;
        for (int i = 0; i < 10; ++i) {
            list.push_back(i);
        }
        auto it = list.begin();
        std::advance(it,3);
        std::cout << *it << std::endl;
        std::cout << *std::next(it) << std::endl;
        std::cout << *it << std::endl;
        std::reverse_iterator<std::list<int>::iterator> rit(it);
        std::cout << *(rit++) << std::endl;

        auto back_it = std::back_inserter(list);
        back_it = 6;
        back_it = 7;
        back_it = 8;
        for (const auto &item : list) {
            std::cout << item << " ";
        }
        std::cout << std::endl;
    }

    void Test(){
        //    stl_test::test_stack();  // 测试栈
        //    stl_test::test_queue();  // 测试队列
        //    stl_test::test_priority_queue(); // 测试优先级队列
        //    stl_test::test_array();
        //    stl_test::test_vector();
        //    stl_test::test_deque();
        //    stl_test::test_list();
    }
}

#endif //DATASTRUCTURE_STL_TEST_H
